package com.rhaggarty.blackjack;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.rhaggarty.blackjack.cardroom.Dealer;
import com.rhaggarty.blackjack.cardroom.Seat;
import com.rhaggarty.blackjack.cardroom.SinglePlayerDealer;
import com.rhaggarty.blackjack.game.GameAction;
import com.rhaggarty.blackjack.game.GameResult;
import com.rhaggarty.blackjack.game.GameStatus;
import com.rhaggarty.blackjack.game.Player;

public class Blackjack extends Activity implements OnClickListener {
    
    private static final Map<Integer, GameAction> buttons;
    
    private final Dealer dealer = new SinglePlayerDealer();
    
    private TextView logger;
    
    private Seat seat;
    
    private TextView stackView;
    
    static {
        buttons = new HashMap<Integer, GameAction>();
        buttons.put(R.id.bet, GameAction.BET);
        buttons.put(R.id.hit, GameAction.HIT);
        buttons.put(R.id.doubleDown, GameAction.DOUBLE_DOWN);
        buttons.put(R.id.stand, GameAction.STAND);
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        logger = (TextView) findViewById(R.id.logView);
        stackView = (TextView) findViewById(R.id.playerStack);
        
        for (final Integer entry : buttons.keySet()) {
            ((Button) findViewById(entry)).setOnClickListener(this);
        }
        
        init();
    }
    
    public void onClick(final View view) {
        final GameAction action = buttons.get(view.getId());
        validate(action);
        
        log(action.toString());
        GameResult result = null;
        switch (action) {
        case BET:
            result = dealer.bet(BigDecimal.TEN);
            break;
        case HIT:
            result = dealer.hit();
            break;
        case STAND:
            result = dealer.stand();
            break;
        }
        
        handleResult(result);
    }
    
    private void log(final String text) {
        logger.setText("Log: " + text);
    }
    
    private void init() {
        final Player player = new Player(new BigDecimal("1000.00"));
        seat = dealer.sitPlayer(player);
        stackView.setText(player.getStack().toString());
    }

    private void handleResult(final GameResult result) {
    	log(result.toString());
        
        displayPlayerHand();
        stackView.setText(seat.getPlayer().getStack().toString());
        updateAvailableButtons(result.getStatus());
    }
    
    private void displayPlayerHand() {
        ((TextView) findViewById(R.id.playerLabel)).setText("Player shows: " + seat.getHand() + " -- " + seat.getHand().getFormattedValues());
    }
    
    private void validate(final GameAction action) {
    	
    }
    
    private void updateAvailableButtons(final GameStatus status) {
    	switch (status) {
    	case CLEAN:
    	case BUST:
    	case PUSH:
    	case WIN:
    	case BLACKJACK:
    	case LOSS:
    		toggleButton(false, R.id.doubleDown, R.id.hit, R.id.stand);
    		toggleButton(true, R.id.bet);
    		break;
    	case PENDING:
    		toggleButton(true, R.id.doubleDown, R.id.hit, R.id.stand);
    		toggleButton(false, R.id.bet);
    		break;
    	}
    }
    
    private void toggleButton(final boolean enabled, final int ... ids) {
    	for (final int id : ids) {
    		((Button) findViewById(id)).setEnabled(enabled);
    	}
    }
}
